package com.hh.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.demo.entity.User;
import com.hh.demo.service.IUserService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吉吉国王
 * @since 2022-12-05
 */
@Controller
public class UserController {
    @Resource
    private IUserService userService;

    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    @PostMapping("/userData")
    @ResponseBody
    public void saveUserData(String userid,String name,String address,String phone,String points,String cash){
        User user = new User();
        user.setId(userid);
        user.setName(name);
        user.setAddress(address);
        user.setPhone(phone);
        user.setPoints(points);
        user.setCash(cash);
        user.setDate(format.format(date));
        userService.save(user);
    }

    //查询全部信息*
    @RequestMapping("/queryAllUser")
    public String queryAllUser(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 5);//设置页面的位置和展示的数据条目数
        List<User> list = userService.list();
        PageInfo<User> pageInfo = new PageInfo<>(list);//PageInfo用来封装页面信息，返回给前台界面
        model.addAttribute("pageInfo", pageInfo);
        return "queryAllUser";
    }
    //查询会员积分
    @RequestMapping("/queryPoint")
    @ResponseBody
    public List<User> queryPoint(){
        return userService.queryPoint();
    }
}
